package com.wechatapp.mengyao.poetry.service.impl;

import com.wechatapp.mengyao.poetry.core.CurrentContext;
import com.wechatapp.mengyao.poetry.core.OperatingUser;
import com.wechatapp.mengyao.poetry.core.PageResult;
import com.wechatapp.mengyao.poetry.vo.ChinesePoetryPageView;
import com.wechatapp.mengyao.poetry.entity.Favorite;
import com.wechatapp.mengyao.poetry.entity.PoetryType;
import com.wechatapp.mengyao.poetry.mapper.FavoriteMapper;
import com.wechatapp.mengyao.poetry.mapper.PoetryTypeMapper;
import com.wechatapp.mengyao.poetry.service.ChinesePoetryService;
import com.wechatapp.mengyao.poetry.service.FavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * @author yanghuaxu
 * @date 2025/5/21 11:02
 */
@Service
@Slf4j
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;
    @Autowired
    private ChinesePoetryService chinesePoetryService;
    @Autowired
    private PoetryTypeMapper poetryTypeMapper;

    @Override
    public void addFavorite(Integer poetryId) {
        Assert.notNull(poetryId, "诗歌不能为空");
        if (!ObjectUtils.isEmpty(getCurrentUserFavoritePoetry(poetryId))) {
            log.info("诗歌已被收藏，忽略收藏操作");
            return;
        }
        OperatingUser user = CurrentContext.getCurrentOperatingUser();
        Favorite favorite = new Favorite();
        favorite.setUserId(user.getUserId());
        favorite.setPoetryId(poetryId);
        favoriteMapper.insertSelective(favorite);
        log.info("收藏诗歌成功");
    }

    @Override
    public void cancelFavorite(Integer poetryId) {
        Assert.notNull(poetryId, "诗歌不能为空");
        OperatingUser user = CurrentContext.getCurrentOperatingUser();
        Favorite currentUserFavoritePoetry = getCurrentUserFavoritePoetry(poetryId);
        if (ObjectUtils.isEmpty(currentUserFavoritePoetry)) {
            return;
        }
        favoriteMapper.deleteByPrimaryKey(currentUserFavoritePoetry.getId());
        log.info("取消收藏诗歌成功");
    }

    @Override
    public Favorite getCurrentUserFavoritePoetry(Integer poetryId) {
        OperatingUser user = CurrentContext.getCurrentOperatingUser();
        Favorite condition = new Favorite();
        condition.setUserId(user.getUserId());
        condition.setPoetryId(poetryId);
        return favoriteMapper.selectOne(condition);
    }

    @Override
    public PageResult<ChinesePoetryPageView> getFavoriteList(Map<String, String> map) {
        map.put("isFavorite", "1");
        return chinesePoetryService.listByPage(map);
    }

    @Override
    public List<PoetryType> getCurrentUserFavoriteTypeList() {
        OperatingUser user = CurrentContext.getCurrentOperatingUser();
        return poetryTypeMapper.getCurrentUserFavoriteTypeList(user.getUserId());
    }
}
